package com.apestech.api.workflow.extend;

import com.apestech.api.workflow.modul.AddSignModul;
import com.apestech.framework.json.JSON;


/**
 * 加签
 *
 * @author hup
 * @create 2018-07-11-9:43
 */
public class AddSign {

    private AddSignModul claim=new AddSignModul();
    private boolean finish=false;

    public void setClaim(String text) throws Exception {
        claim=new AddSignModul(text);
    }

    public String getClaimStr() throws Exception {
        return JSON.toJSONString(claim);
    }

    public AddSignModul getClaim() throws Exception {
        return claim;
    }

    public boolean isFinish() {
        return finish;
    }

    public void start(String fromUserId, String fromUserName, String toUserId, String toUserName, String candidateId, String candidateName) throws Exception {
        if(fromUserId.equals(toUserId)){
            throw new Exception("加签发起人和被加签人不允许为同一人["+fromUserId+"]！");
        }
        claim.setUserId(fromUserId);
        claim.setUserName(fromUserName);
        claim.add(toUserId, toUserName, candidateId, candidateName);
        claim.setOrder(0);
        claim.addInUser(toUserId);
        if(!toUserId.equals(candidateId)){
            claim.addInUser(candidateId);
        }
    }

    public void addUser(String fromUserId, String toUserId, String toUserName, String candidateId, String candidateName) throws Exception {
        int size=claim.size()-1;
        if(claim.getOrder()<size){
            throw new Exception("该操作在回复阶段，不允许加签，如要加签：\r\n"+
                    "可在意见中记录需要追加新的参与人相关信息，由原发起人再发起新的加签操作");
        }
        if(!fromUserId.equals(claim.getId(claim.getOrder())) && !fromUserId.equals(claim.getCandidateId(claim.getOrder()))  ){
            throw new Exception("发起人["+fromUserId+"]与链上发起人["+claim.getId(claim.getOrder())+"、"+claim.getCandidateId(claim.getOrder())+"]不符，不允许加签操作！");
        }
        if((","+claim.getInUser()+",").indexOf(","+toUserId+",")>=0){
            throw new Exception("该用户["+toUserId+"]已在加签流程线上，不允许再次追加加签操作！");
        }
        claim.add(toUserId, toUserName, candidateId, candidateName);
        claim.setOrder(claim.getOrder()+1);
        claim.addInUser(toUserId);
        if(!toUserId.equals(candidateId)){
            claim.addInUser(candidateId);
        }
    }

    public String execUser(String fromUserId) throws Exception {
        String id=claim.getId(claim.getOrder());
        if(!id.equals(fromUserId)){
            if(!fromUserId.equals(claim.getId(claim.getOrder())) && !fromUserId.equals(claim.getCandidateId(claim.getOrder()))  ){
                throw new Exception("发起人["+fromUserId+"]与链上发起人["+claim.getId(claim.getOrder())+"、"+claim.getCandidateId(claim.getOrder())+"]不符，不允许加签回应操作！");
            }
        }
        int order=claim.getOrder();
        String userId=null;
        if(order==0){
            userId=claim.getUserId();
            finish=true;
        } else {
            userId=claim.getUserlist().get(claim.getOrder()-1).getId();
        }
        claim.setOrder(claim.getOrder()-1);
        return userId;
    }

    public void changeUser(String fromUserId, String toUserId, String toUserName, String candidateId, String candidateName) throws Exception {
        String id=claim.getId(claim.getOrder());
        if(!id.equals(fromUserId)){
            if(!fromUserId.equals(claim.getId(claim.getOrder())) && !fromUserId.equals(claim.getCandidateId(claim.getOrder()))  ){
                throw new Exception("发起人["+fromUserId+"]与链上发起人["+claim.getId(claim.getOrder())+"、"+claim.getCandidateId(claim.getOrder())+"]不符，不允许加签转签操作！");
            }
        }
        claim.set(claim.getOrder(), toUserId, toUserName, candidateId, candidateName);
        String inUser="";
        int size=claim.size();
        for(int i=0; i<size; i++){
            String u=claim.getId(i);
            if((","+inUser+",").indexOf(","+u+",")<0){
                if(!"".equals(inUser)){
                    inUser=inUser+",";
                }
                inUser=inUser+u;
            }
        }
        claim.setInUser(inUser);
    }

}
